home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / inptport.c < prev    next >
C/C++ Source or Header  |  2000-04-24  |  57KB  |  1,788 lines

  1. /***************************************************************************
  2.  
  3.   inptport.c
  4.  
  5.   Input ports handling
  6.  
  7. TODO:    remove the 1 analog device per port limitation
  8.         support for inputports producing interrupts
  9.         support for extra "real" hardware (PC throttle's, spinners etc)
  10.  
  11. ***************************************************************************/
  12.  
  13. #include "driver.h"
  14. #include <math.h>
  15.  
  16. #ifdef MAME_NET
  17. #include "network.h"
  18.  
  19. static unsigned short input_port_defaults[MAX_INPUT_PORTS];
  20. static int default_player;
  21. static int analog_player_port[MAX_INPUT_PORTS];
  22. #endif /* MAME_NET */
  23.  
  24. /* Use the MRU code for 4way joysticks */
  25. #define MRU_JOYSTICK
  26.  
  27. /* header identifying the version of the game.cfg file */
  28. /* mame 0.36b11 */
  29. #define MAMECFGSTRING_V5 "MAMECFG\5"
  30. #define MAMEDEFSTRING_V5 "MAMEDEF\4"
  31.  
  32. /* mame 0.36b12 with multi key/joy extension */
  33. #define MAMECFGSTRING_V6 "MAMECFG\6"
  34. #define MAMEDEFSTRING_V6 "MAMEDEF\5"
  35.  
  36. /* mame 0.36b13 with and/or/not combination */
  37. #define MAMECFGSTRING_V7 "MAMECFG\7"
  38. #define MAMEDEFSTRING_V7 "MAMEDEF\6"
  39.  
  40. /* mame 0.36b16 with key/joy merge */
  41. #define MAMECFGSTRING_V8 "MAMECFG\x8"
  42. #define MAMEDEFSTRING_V8 "MAMEDEF\7"
  43.  
  44. extern void *record;
  45. extern void *playback;
  46.  
  47. extern unsigned int dispensed_tickets;
  48. extern unsigned int coins[COIN_COUNTERS];
  49. extern unsigned int lastcoin[COIN_COUNTERS];
  50. extern unsigned int coinlockedout[COIN_COUNTERS];
  51.  
  52. static unsigned short input_port_value[MAX_INPUT_PORTS];
  53. static unsigned short input_vblank[MAX_INPUT_PORTS];
  54.  
  55. /* Assuming a maxium of one analog input device per port BW 101297 */
  56. static struct InputPort *input_analog[MAX_INPUT_PORTS];
  57. static int input_analog_current_value[MAX_INPUT_PORTS],input_analog_previous_value[MAX_INPUT_PORTS];
  58. static int input_analog_init[MAX_INPUT_PORTS];
  59.  
  60. static int mouse_delta_x[OSD_MAX_JOY_ANALOG], mouse_delta_y[OSD_MAX_JOY_ANALOG];
  61. static int analog_current_x[OSD_MAX_JOY_ANALOG], analog_current_y[OSD_MAX_JOY_ANALOG];
  62. static int analog_previous_x[OSD_MAX_JOY_ANALOG], analog_previous_y[OSD_MAX_JOY_ANALOG];
  63.  
  64.  
  65. /***************************************************************************
  66.  
  67.   Configuration load/save
  68.  
  69. ***************************************************************************/
  70.  
  71. /* this must match the enum in inptport.h */
  72. char ipdn_defaultstrings[][MAX_DEFSTR_LEN] =
  73. {
  74.     "Off",
  75.     "On",
  76.     "No",
  77.     "Yes",
  78.     "Lives",
  79.     "Bonus Life",
  80.     "Difficulty",
  81.     "Demo Sounds",
  82.     "Coinage",
  83.     "Coin A",
  84.     "Coin B",
  85.     "9 Coins/1 Credit",
  86.     "8 Coins/1 Credit",
  87.     "7 Coins/1 Credit",
  88.     "6 Coins/1 Credit",
  89.     "5 Coins/1 Credit",
  90.     "4 Coins/1 Credit",
  91.     "3 Coins/1 Credit",
  92.     "8 Coins/3 Credits",
  93.     "4 Coins/2 Credits",
  94.     "2 Coins/1 Credit",
  95.     "5 Coins/3 Credits",
  96.     "3 Coins/2 Credits",
  97.     "4 Coins/3 Credits",
  98.     "4 Coins/4 Credits",
  99.     "3 Coins/3 Credits",
  100.     "2 Coins/2 Credits",
  101.     "1 Coin/1 Credit",
  102.     "4 Coins/5 Credits",
  103.     "3 Coins/4 Credits",
  104.     "2 Coins/3 Credits",
  105.     "4 Coins/7 Credits",
  106.     "2 Coins/4 Credits",
  107.     "1 Coin/2 Credits",
  108.     "2 Coins/5 Credits",
  109.     "2 Coins/6 Credits",
  110.     "1 Coin/3 Credits",
  111.     "2 Coins/7 Credits",
  112.     "2 Coins/8 Credits",
  113.     "1 Coin/4 Credits",
  114.     "1 Coin/5 Credits",
  115.     "1 Coin/6 Credits",
  116.     "1 Coin/7 Credits",
  117.     "1 Coin/8 Credits",
  118.     "1 Coin/9 Credits",
  119.     "Free Play",
  120.     "Cabinet",
  121.     "Upright",
  122.     "Cocktail",
  123.     "Flip Screen",
  124.     "Service Mode",
  125.     "Unused",
  126.     "Unknown"    /* must be the last one, mame.c relies on that */
  127. };
  128.  
  129. struct ipd inputport_defaults[] =
  130. {
  131.     { IPT_UI_CONFIGURE,         "Config Menu",       SEQ_DEF_1(KEYCODE_TAB) },
  132.     { IPT_UI_ON_SCREEN_DISPLAY, "On Screen Display", SEQ_DEF_1(KEYCODE_TILDE) },
  133.     { IPT_UI_PAUSE,             "Pause",             SEQ_DEF_1(KEYCODE_P) },
  134.     { IPT_UI_RESET_MACHINE,     "Reset Game",        SEQ_DEF_1(KEYCODE_F3) },
  135.     { IPT_UI_SHOW_GFX,          "Show Gfx",          SEQ_DEF_1(KEYCODE_F4) },
  136.     { IPT_UI_FRAMESKIP_DEC,     "Frameskip Dec",     SEQ_DEF_1(KEYCODE_F8) },
  137.     { IPT_UI_FRAMESKIP_INC,     "Frameskip Inc",     SEQ_DEF_1(KEYCODE_F9) },
  138.     { IPT_UI_THROTTLE,          "Throttle",          SEQ_DEF_1(KEYCODE_F10) },
  139.     { IPT_UI_SHOW_FPS,          "Show FPS",          SEQ_DEF_5(KEYCODE_F11, CODE_NOT, KEYCODE_LCONTROL, CODE_NOT, KEYCODE_LSHIFT) },
  140.     { IPT_UI_SHOW_PROFILER,     "Show Profiler",     SEQ_DEF_2(KEYCODE_F11, KEYCODE_LSHIFT) },
  141. #ifdef MAME_DEBUG
  142.     { IPT_UI_SHOW_COLORS,       "Show Colors",     SEQ_DEF_2(KEYCODE_F11, KEYCODE_LCONTROL) },
  143. #endif
  144. #ifdef MESS
  145.     { IPT_UI_TOGGLE_UI,         "UI Toggle",         SEQ_DEF_1(KEYCODE_SCRLOCK) },
  146. #endif
  147.     { IPT_UI_SNAPSHOT,          "Save Snapshot",     SEQ_DEF_1(KEYCODE_F12) },
  148.     { IPT_UI_TOGGLE_CHEAT,      "Toggle Cheat",      SEQ_DEF_1(KEYCODE_F5) },
  149.     { IPT_UI_UP,                "UI Up",             SEQ_DEF_3(KEYCODE_UP, CODE_OR, JOYCODE_1_UP) },
  150.     { IPT_UI_DOWN,              "UI Down",           SEQ_DEF_3(KEYCODE_DOWN, CODE_OR, JOYCODE_1_DOWN) },
  151.     { IPT_UI_LEFT,              "UI Left",           SEQ_DEF_3(KEYCODE_LEFT, CODE_OR, JOYCODE_1_LEFT) },
  152.     { IPT_UI_RIGHT,             "UI Right",          SEQ_DEF_3(KEYCODE_RIGHT, CODE_OR, JOYCODE_1_RIGHT) },
  153.     { IPT_UI_SELECT,            "UI Select",         SEQ_DEF_3(KEYCODE_ENTER, CODE_OR, JOYCODE_1_BUTTON1) },
  154.     { IPT_UI_CANCEL,            "UI Cancel",         SEQ_DEF_1(KEYCODE_ESC) },
  155.     { IPT_UI_PAN_UP,            "Pan Up",            SEQ_DEF_3(KEYCODE_PGUP, CODE_NOT, KEYCODE_LSHIFT) },
  156.     { IPT_UI_PAN_DOWN,          "Pan Down",          SEQ_DEF_3(KEYCODE_PGDN, CODE_NOT, KEYCODE_LSHIFT) },
  157.     { IPT_UI_PAN_LEFT,          "Pan Left",          SEQ_DEF_2(KEYCODE_PGUP, KEYCODE_LSHIFT) },
  158.     { IPT_UI_PAN_RIGHT,         "Pan Right",         SEQ_DEF_2(KEYCODE_PGDN, KEYCODE_LSHIFT) },
  159.     { IPT_START1, "1 Player Start",  SEQ_DEF_1(KEYCODE_1) },
  160.     { IPT_START2, "2 Players Start", SEQ_DEF_1(KEYCODE_2) },
  161.     { IPT_START3, "3 Players Start", SEQ_DEF_1(KEYCODE_3) },
  162.     { IPT_START4, "4 Players Start", SEQ_DEF_1(KEYCODE_4) },
  163.     { IPT_COIN1,  "Coin 1",          SEQ_DEF_1(KEYCODE_5) },
  164.     { IPT_COIN2,  "Coin 2",          SEQ_DEF_1(KEYCODE_6) },
  165.     { IPT_COIN3,  "Coin 3",          SEQ_DEF_1(KEYCODE_7) },
  166.     { IPT_COIN4,  "Coin 4",          SEQ_DEF_1(KEYCODE_8) },
  167.     { IPT_SERVICE1, "Service 1",     SEQ_DEF_1(KEYCODE_9) },
  168.     { IPT_SERVICE2, "Service 2",     SEQ_DEF_1(KEYCODE_0) },
  169.     { IPT_SERVICE3, "Service 3",     SEQ_DEF_1(KEYCODE_MINUS) },
  170.     { IPT_SERVICE4, "Service 4",     SEQ_DEF_1(KEYCODE_EQUALS) },
  171.     { IPT_TILT,   "Tilt",            SEQ_DEF_1(KEYCODE_T) },
  172.  
  173.     { IPT_JOYSTICK_UP         | IPF_PLAYER1, "P1 Up",          SEQ_DEF_3(KEYCODE_UP, CODE_OR, JOYCODE_1_UP)    },
  174.     { IPT_JOYSTICK_DOWN       | IPF_PLAYER1, "P1 Down",        SEQ_DEF_3(KEYCODE_DOWN, CODE_OR, JOYCODE_1_DOWN)  },
  175.     { IPT_JOYSTICK_LEFT       | IPF_PLAYER1, "P1 Left",        SEQ_DEF_3(KEYCODE_LEFT, CODE_OR, JOYCODE_1_LEFT)  },
  176.     { IPT_JOYSTICK_RIGHT      | IPF_PLAYER1, "P1 Right",       SEQ_DEF_3(KEYCODE_RIGHT, CODE_OR, JOYCODE_1_RIGHT) },
  177.     { IPT_BUTTON1             | IPF_PLAYER1, "P1 Button 1",    SEQ_DEF_3(KEYCODE_LCONTROL, CODE_OR, JOYCODE_1_BUTTON1) },
  178.     { IPT_BUTTON2             | IPF_PLAYER1, "P1 Button 2",    SEQ_DEF_3(KEYCODE_LALT, CODE_OR, JOYCODE_1_BUTTON2) },
  179.     { IPT_BUTTON3             | IPF_PLAYER1, "P1 Button 3",    SEQ_DEF_3(KEYCODE_SPACE, CODE_OR, JOYCODE_1_BUTTON3) },
  180.     { IPT_BUTTON4             | IPF_PLAYER1, "P1 Button 4",    SEQ_DEF_3(KEYCODE_LSHIFT, CODE_OR, JOYCODE_1_BUTTON4) },
  181.     { IPT_BUTTON5             | IPF_PLAYER1, "P1 Button 5",    SEQ_DEF_3(KEYCODE_Z, CODE_OR, JOYCODE_1_BUTTON5) },
  182.     { IPT_BUTTON6             | IPF_PLAYER1, "P1 Button 6",    SEQ_DEF_3(KEYCODE_X, CODE_OR, JOYCODE_1_BUTTON6) },
  183.     { IPT_BUTTON7             | IPF_PLAYER1, "P1 Button 7",    SEQ_DEF_1(KEYCODE_C) },
  184.     { IPT_BUTTON8             | IPF_PLAYER1, "P1 Button 8",    SEQ_DEF_1(KEYCODE_V) },
  185.     { IPT_BUTTON9             | IPF_PLAYER1, "P1 Button 9",    SEQ_DEF_1(KEYCODE_B) },
  186.     { IPT_JOYSTICKRIGHT_UP    | IPF_PLAYER1, "P1 Right/Up",    SEQ_DEF_3(KEYCODE_I, CODE_OR, JOYCODE_1_BUTTON2) },
  187.     { IPT_JOYSTICKRIGHT_DOWN  | IPF_PLAYER1, "P1 Right/Down",  SEQ_DEF_3(KEYCODE_K, CODE_OR, JOYCODE_1_BUTTON3) },
  188.     { IPT_JOYSTICKRIGHT_LEFT  | IPF_PLAYER1, "P1 Right/Left",  SEQ_DEF_3(KEYCODE_J, CODE_OR, JOYCODE_1_BUTTON1) },
  189.     { IPT_JOYSTICKRIGHT_RIGHT | IPF_PLAYER1, "P1 Right/Right", SEQ_DEF_3(KEYCODE_L, CODE_OR, JOYCODE_1_BUTTON4) },
  190.     { IPT_JOYSTICKLEFT_UP     | IPF_PLAYER1, "P1 Left/Up",     SEQ_DEF_3(KEYCODE_E, CODE_OR, JOYCODE_1_UP) },
  191.     { IPT_JOYSTICKLEFT_DOWN   | IPF_PLAYER1, "P1 Left/Down",   SEQ_DEF_3(KEYCODE_D, CODE_OR, JOYCODE_1_DOWN) },
  192.     { IPT_JOYSTICKLEFT_LEFT   | IPF_PLAYER1, "P1 Left/Left",   SEQ_DEF_3(KEYCODE_S, CODE_OR, JOYCODE_1_LEFT) },
  193.     { IPT_JOYSTICKLEFT_RIGHT  | IPF_PLAYER1, "P1 Left/Right",  SEQ_DEF_3(KEYCODE_F, CODE_OR, JOYCODE_1_RIGHT) },
  194.  
  195.     { IPT_JOYSTICK_UP         | IPF_PLAYER2, "P2 Up",          SEQ_DEF_3(KEYCODE_R, CODE_OR, JOYCODE_2_UP)    },
  196.     { IPT_JOYSTICK_DOWN       | IPF_PLAYER2, "P2 Down",        SEQ_DEF_3(KEYCODE_F, CODE_OR, JOYCODE_2_DOWN)  },
  197.     { IPT_JOYSTICK_LEFT       | IPF_PLAYER2, "P2 Left",        SEQ_DEF_3(KEYCODE_D, CODE_OR, JOYCODE_2_LEFT)  },
  198.     { IPT_JOYSTICK_RIGHT      | IPF_PLAYER2, "P2 Right",       SEQ_DEF_3(KEYCODE_G, CODE_OR, JOYCODE_2_RIGHT) },
  199.     { IPT_BUTTON1             | IPF_PLAYER2, "P2 Button 1",    SEQ_DEF_3(KEYCODE_A, CODE_OR, JOYCODE_2_BUTTON1) },
  200.     { IPT_BUTTON2             | IPF_PLAYER2, "P2 Button 2",    SEQ_DEF_3(KEYCODE_S, CODE_OR, JOYCODE_2_BUTTON2) },
  201.     { IPT_BUTTON3             | IPF_PLAYER2, "P2 Button 3",    SEQ_DEF_3(KEYCODE_Q, CODE_OR, JOYCODE_2_BUTTON3) },
  202.     { IPT_BUTTON4             | IPF_PLAYER2, "P2 Button 4",    SEQ_DEF_3(KEYCODE_W, CODE_OR, JOYCODE_2_BUTTON4) },
  203.     { IPT_BUTTON5             | IPF_PLAYER2, "P2 Button 5",    SEQ_DEF_1(JOYCODE_2_BUTTON5) },
  204.     { IPT_BUTTON6             | IPF_PLAYER2, "P2 Button 6",    SEQ_DEF_1(JOYCODE_2_BUTTON6) },
  205.     { IPT_BUTTON7             | IPF_PLAYER2, "P2 Button 7",    SEQ_DEF_0 },
  206.     { IPT_BUTTON8             | IPF_PLAYER2, "P2 Button 8",    SEQ_DEF_0 },
  207.     { IPT_BUTTON9             | IPF_PLAYER2, "P2 Button 9",    SEQ_DEF_0 },
  208.     { IPT_JOYSTICKRIGHT_UP    | IPF_PLAYER2, "P2 Right/Up",    SEQ_DEF_0 },
  209.     { IPT_JOYSTICKRIGHT_DOWN  | IPF_PLAYER2, "P2 Right/Down",  SEQ_DEF_0 },
  210.     { IPT_JOYSTICKRIGHT_LEFT  | IPF_PLAYER2, "P2 Right/Left",  SEQ_DEF_0 },
  211.     { IPT_JOYSTICKRIGHT_RIGHT | IPF_PLAYER2, "P2 Right/Right", SEQ_DEF_0 },
  212.     { IPT_JOYSTICKLEFT_UP     | IPF_PLAYER2, "P2 Left/Up",     SEQ_DEF_0 },
  213.     { IPT_JOYSTICKLEFT_DOWN   | IPF_PLAYER2, "P2 Left/Down",   SEQ_DEF_0 },
  214.     { IPT_JOYSTICKLEFT_LEFT   | IPF_PLAYER2, "P2 Left/Left",   SEQ_DEF_0 },
  215.     { IPT_JOYSTICKLEFT_RIGHT  | IPF_PLAYER2, "P2 Left/Right",  SEQ_DEF_0 },
  216.  
  217.     { IPT_JOYSTICK_UP         | IPF_PLAYER3, "P3 Up",          SEQ_DEF_3(KEYCODE_I, CODE_OR, JOYCODE_3_UP)    },
  218.     { IPT_JOYSTICK_DOWN       | IPF_PLAYER3, "P3 Down",        SEQ_DEF_3(KEYCODE_K, CODE_OR, JOYCODE_3_DOWN)  },
  219.     { IPT_JOYSTICK_LEFT       | IPF_PLAYER3, "P3 Left",        SEQ_DEF_3(KEYCODE_J, CODE_OR, JOYCODE_3_LEFT)  },
  220.     { IPT_JOYSTICK_RIGHT      | IPF_PLAYER3, "P3 Right",       SEQ_DEF_3(KEYCODE_L, CODE_OR, JOYCODE_3_RIGHT) },
  221.     { IPT_BUTTON1             | IPF_PLAYER3, "P3 Button 1",    SEQ_DEF_3(KEYCODE_RCONTROL, CODE_OR, JOYCODE_3_BUTTON1) },
  222.     { IPT_BUTTON2             | IPF_PLAYER3, "P3 Button 2",    SEQ_DEF_3(KEYCODE_RSHIFT, CODE_OR, JOYCODE_3_BUTTON2) },
  223.     { IPT_BUTTON3             | IPF_PLAYER3, "P3 Button 3",    SEQ_DEF_3(KEYCODE_ENTER, CODE_OR, JOYCODE_3_BUTTON3) },
  224.     { IPT_BUTTON4             | IPF_PLAYER3, "P3 Button 4",    SEQ_DEF_1(JOYCODE_3_BUTTON4) },
  225.  
  226.     { IPT_JOYSTICK_UP         | IPF_PLAYER4, "P4 Up",          SEQ_DEF_1(JOYCODE_4_UP) },
  227.     { IPT_JOYSTICK_DOWN       | IPF_PLAYER4, "P4 Down",        SEQ_DEF_1(JOYCODE_4_DOWN) },
  228.     { IPT_JOYSTICK_LEFT       | IPF_PLAYER4, "P4 Left",        SEQ_DEF_1(JOYCODE_4_LEFT) },
  229.     { IPT_JOYSTICK_RIGHT      | IPF_PLAYER4, "P4 Right",       SEQ_DEF_1(JOYCODE_4_RIGHT) },
  230.     { IPT_BUTTON1             | IPF_PLAYER4, "P4 Button 1",    SEQ_DEF_1(JOYCODE_4_BUTTON1) },
  231.     { IPT_BUTTON2             | IPF_PLAYER4, "P4 Button 2",    SEQ_DEF_1(JOYCODE_4_BUTTON2) },
  232.     { IPT_BUTTON3             | IPF_PLAYER4, "P4 Button 3",    SEQ_DEF_1(JOYCODE_4_BUTTON3) },
  233.     { IPT_BUTTON4             | IPF_PLAYER4, "P4 Button 4",    SEQ_DEF_1(JOYCODE_4_BUTTON4) },
  234.  
  235.     { IPT_PEDAL                    | IPF_PLAYER1, "Pedal 1",        SEQ_DEF_3(KEYCODE_LCONTROL, CODE_OR, JOYCODE_1_BUTTON1) },
  236.     { (IPT_PEDAL+IPT_EXTENSION) | IPF_PLAYER1, "P1 Auto Release <Y/N>", SEQ_DEF_1(KEYCODE_Y) },
  237.     { IPT_PEDAL                 | IPF_PLAYER2, "Pedal 2",        SEQ_DEF_3(KEYCODE_A, CODE_OR, JOYCODE_2_BUTTON1) },
  238.     { (IPT_PEDAL+IPT_EXTENSION) | IPF_PLAYER2, "P2 Auto Release <Y/N>", SEQ_DEF_1(KEYCODE_Y) },
  239.     { IPT_PEDAL                 | IPF_PLAYER3, "Pedal 3",        SEQ_DEF_3(KEYCODE_RCONTROL, CODE_OR, JOYCODE_3_BUTTON1) },
  240.     { (IPT_PEDAL+IPT_EXTENSION) | IPF_PLAYER3, "P3 Auto Release <Y/N>", SEQ_DEF_1(KEYCODE_Y) },
  241.     { IPT_PEDAL                 | IPF_PLAYER4, "Pedal 4",        SEQ_DEF_1(JOYCODE_4_BUTTON1) },
  242.     { (IPT_PEDAL+IPT_EXTENSION) | IPF_PLAYER4, "P4 Auto Release <Y/N>", SEQ_DEF_1(KEYCODE_Y) },
  243.  
  244.     { IPT_PADDLE | IPF_PLAYER1,  "Paddle",        SEQ_DEF_3(KEYCODE_LEFT, CODE_OR, JOYCODE_1_LEFT) },
  245.     { (IPT_PADDLE | IPF_PLAYER1)+IPT_EXTENSION,             "Paddle",        SEQ_DEF_3(KEYCODE_RIGHT, CODE_OR, JOYCODE_1_RIGHT)  },
  246.     { IPT_PADDLE | IPF_PLAYER2,  "Paddle 2",      SEQ_DEF_3(KEYCODE_D, CODE_OR, JOYCODE_2_LEFT) },
  247.     { (IPT_PADDLE | IPF_PLAYER2)+IPT_EXTENSION,             "Paddle 2",      SEQ_DEF_3(KEYCODE_G, CODE_OR, JOYCODE_2_RIGHT) },
  248.     { IPT_PADDLE | IPF_PLAYER3,  "Paddle 3",      SEQ_DEF_3(KEYCODE_J, CODE_OR, JOYCODE_3_LEFT) },
  249.     { (IPT_PADDLE | IPF_PLAYER3)+IPT_EXTENSION,             "Paddle 3",      SEQ_DEF_3(KEYCODE_L, CODE_OR, JOYCODE_3_RIGHT) },
  250.     { IPT_PADDLE | IPF_PLAYER4,  "Paddle 4",      SEQ_DEF_1(JOYCODE_4_LEFT) },
  251.     { (IPT_PADDLE | IPF_PLAYER4)+IPT_EXTENSION,             "Paddle 4",      SEQ_DEF_1(JOYCODE_4_RIGHT) },
  252.     { IPT_PADDLE_V | IPF_PLAYER1,  "Paddle V",          SEQ_DEF_3(KEYCODE_UP, CODE_OR, JOYCODE_1_UP) },
  253.     { (IPT_PADDLE_V | IPF_PLAYER1)+IPT_EXTENSION,             "Paddle V",          SEQ_DEF_3(KEYCODE_DOWN, CODE_OR, JOYCODE_1_DOWN) },
  254.     { IPT_PADDLE_V | IPF_PLAYER2,  "Paddle V 2",        SEQ_DEF_3(KEYCODE_R, CODE_OR, JOYCODE_2_UP) },
  255.     { (IPT_PADDLE_V | IPF_PLAYER2)+IPT_EXTENSION,             "Paddle V 2",      SEQ_DEF_3(KEYCODE_F, CODE_OR, JOYCODE_2_DOWN) },
  256.     { IPT_PADDLE_V | IPF_PLAYER3,  "Paddle V 3",        SEQ_DEF_3(KEYCODE_I, CODE_OR, JOYCODE_3_UP) },
  257.     { (IPT_PADDLE_V | IPF_PLAYER3)+IPT_EXTENSION,             "Paddle V 3",      SEQ_DEF_3(KEYCODE_K, CODE_OR, JOYCODE_3_DOWN) },
  258.     { IPT_PADDLE_V | IPF_PLAYER4,  "Paddle V 4",        SEQ_DEF_1(JOYCODE_4_UP) },
  259.     { (IPT_PADDLE_V | IPF_PLAYER4)+IPT_EXTENSION,             "Paddle V 4",      SEQ_DEF_1(JOYCODE_4_DOWN) },
  260.     { IPT_DIAL | IPF_PLAYER1,    "Dial",          SEQ_DEF_3(KEYCODE_LEFT, CODE_OR, JOYCODE_1_LEFT) },
  261.     { (IPT_DIAL | IPF_PLAYER1)+IPT_EXTENSION,               "Dial",          SEQ_DEF_3(KEYCODE_RIGHT, CODE_OR, JOYCODE_1_RIGHT) },
  262.     { IPT_DIAL | IPF_PLAYER2,    "Dial 2",        SEQ_DEF_3(KEYCODE_D, CODE_OR, JOYCODE_2_LEFT) },
  263.     { (IPT_DIAL | IPF_PLAYER2)+IPT_EXTENSION,               "Dial 2",      SEQ_DEF_3(KEYCODE_G, CODE_OR, JOYCODE_2_RIGHT) },
  264.     { IPT_DIAL | IPF_PLAYER3,    "Dial 3",        SEQ_DEF_3(KEYCODE_J, CODE_OR, JOYCODE_3_LEFT) },
  265.     { (IPT_DIAL | IPF_PLAYER3)+IPT_EXTENSION,               "Dial 3",      SEQ_DEF_3(KEYCODE_L, CODE_OR, JOYCODE_3_RIGHT) },
  266.     { IPT_DIAL | IPF_PLAYER4,    "Dial 4",        SEQ_DEF_1(JOYCODE_4_LEFT) },
  267.     { (IPT_DIAL | IPF_PLAYER4)+IPT_EXTENSION,               "Dial 4",      SEQ_DEF_1(JOYCODE_4_RIGHT) },
  268.     { IPT_DIAL_V | IPF_PLAYER1,  "Dial V",          SEQ_DEF_3(KEYCODE_UP, CODE_OR, JOYCODE_1_UP) },
  269.     { (IPT_DIAL_V | IPF_PLAYER1)+IPT_EXTENSION,             "Dial V",          SEQ_DEF_3(KEYCODE_DOWN, CODE_OR, JOYCODE_1_DOWN) },
  270.     { IPT_DIAL_V | IPF_PLAYER2,  "Dial V 2",        SEQ_DEF_3(KEYCODE_R, CODE_OR, JOYCODE_2_UP) },
  271.     { (IPT_DIAL_V | IPF_PLAYER2)+IPT_EXTENSION,             "Dial V 2",      SEQ_DEF_3(KEYCODE_F, CODE_OR, JOYCODE_2_DOWN) },
  272.     { IPT_DIAL_V | IPF_PLAYER3,  "Dial V 3",        SEQ_DEF_3(KEYCODE_I, CODE_OR, JOYCODE_3_UP) },
  273.     { (IPT_DIAL_V | IPF_PLAYER3)+IPT_EXTENSION,             "Dial V 3",      SEQ_DEF_3(KEYCODE_K, CODE_OR, JOYCODE_3_DOWN) },
  274.     { IPT_DIAL_V | IPF_PLAYER4,  "Dial V 4",        SEQ_DEF_1(JOYCODE_4_UP) },
  275.     { (IPT_DIAL_V | IPF_PLAYER4)+IPT_EXTENSION,             "Dial V 4",      SEQ_DEF_1(JOYCODE_4_DOWN) },
  276.  
  277.     { IPT_TRACKBALL_X | IPF_PLAYER1, "Track X",   SEQ_DEF_3(KEYCODE_LEFT, CODE_OR, JOYCODE_1_LEFT) },
  278.     { (IPT_TRACKBALL_X | IPF_PLAYER1)+IPT_EXTENSION,                 "Track X",   SEQ_DEF_3(KEYCODE_RIGHT, CODE_OR, JOYCODE_1_RIGHT) },
  279.     { IPT_TRACKBALL_X | IPF_PLAYER2, "Track X 2", SEQ_DEF_3(KEYCODE_D, CODE_OR, JOYCODE_2_LEFT) },
  280.     { (IPT_TRACKBALL_X | IPF_PLAYER2)+IPT_EXTENSION,                 "Track X 2", SEQ_DEF_3(KEYCODE_G, CODE_OR, JOYCODE_2_RIGHT) },
  281.     { IPT_TRACKBALL_X | IPF_PLAYER3, "Track X 3", SEQ_DEF_3(KEYCODE_J, CODE_OR, JOYCODE_3_LEFT) },
  282.     { (IPT_TRACKBALL_X | IPF_PLAYER3)+IPT_EXTENSION,                 "Track X 3", SEQ_DEF_3(KEYCODE_L, CODE_OR, JOYCODE_3_RIGHT) },
  283.     { IPT_TRACKBALL_X | IPF_PLAYER4, "Track X 4", SEQ_DEF_1(JOYCODE_4_LEFT) },
  284.     { (IPT_TRACKBALL_X | IPF_PLAYER4)+IPT_EXTENSION,                 "Track X 4", SEQ_DEF_1(JOYCODE_4_RIGHT) },
  285.  
  286.     { IPT_TRACKBALL_Y | IPF_PLAYER1, "Track Y",   SEQ_DEF_3(KEYCODE_UP, CODE_OR, JOYCODE_1_UP) },
  287.     { (IPT_TRACKBALL_Y | IPF_PLAYER1)+IPT_EXTENSION,                 "Track Y",   SEQ_DEF_3(KEYCODE_DOWN, CODE_OR, JOYCODE_1_DOWN) },
  288.     { IPT_TRACKBALL_Y | IPF_PLAYER2, "Track Y 2", SEQ_DEF_3(KEYCODE_R, CODE_OR, JOYCODE_2_UP) },
  289.     { (IPT_TRACKBALL_Y | IPF_PLAYER2)+IPT_EXTENSION,                 "Track Y 2", SEQ_DEF_3(KEYCODE_F, CODE_OR, JOYCODE_2_DOWN) },
  290.     { IPT_TRACKBALL_Y | IPF_PLAYER3, "Track Y 3", SEQ_DEF_3(KEYCODE_I, CODE_OR, JOYCODE_3_UP) },
  291.     { (IPT_TRACKBALL_Y | IPF_PLAYER3)+IPT_EXTENSION,                 "Track Y 3", SEQ_DEF_3(KEYCODE_K, CODE_OR, JOYCODE_3_DOWN) },
  292.     { IPT_TRACKBALL_Y | IPF_PLAYER4, "Track Y 4", SEQ_DEF_1(JOYCODE_4_UP) },
  293.     { (IPT_TRACKBALL_Y | IPF_PLAYER4)+IPT_EXTENSION,                 "Track Y 4", SEQ_DEF_1(JOYCODE_4_DOWN) },
  294.  
  295.     { IPT_AD_STICK_X | IPF_PLAYER1, "AD Stick X",   SEQ_DEF_3(KEYCODE_LEFT, CODE_OR, JOYCODE_1_LEFT) },
  296.     { (IPT_AD_STICK_X | IPF_PLAYER1)+IPT_EXTENSION,                "AD Stick X",   SEQ_DEF_3(KEYCODE_RIGHT, CODE_OR, JOYCODE_1_RIGHT) },
  297.     { IPT_AD_STICK_X | IPF_PLAYER2, "AD Stick X 2", SEQ_DEF_3(KEYCODE_D, CODE_OR, JOYCODE_2_LEFT) },
  298.     { (IPT_AD_STICK_X | IPF_PLAYER2)+IPT_EXTENSION,                "AD Stick X 2", SEQ_DEF_3(KEYCODE_G, CODE_OR, JOYCODE_2_RIGHT) },
  299.     { IPT_AD_STICK_X | IPF_PLAYER3, "AD Stick X 3", SEQ_DEF_3(KEYCODE_J, CODE_OR, JOYCODE_3_LEFT) },
  300.     { (IPT_AD_STICK_X | IPF_PLAYER3)+IPT_EXTENSION,                "AD Stick X 3", SEQ_DEF_3(KEYCODE_L, CODE_OR, JOYCODE_3_RIGHT) },
  301.     { IPT_AD_STICK_X | IPF_PLAYER4, "AD Stick X 4", SEQ_DEF_1(JOYCODE_4_LEFT) },
  302.     { (IPT_AD_STICK_X | IPF_PLAYER4)+IPT_EXTENSION,                "AD Stick X 4", SEQ_DEF_1(JOYCODE_4_RIGHT) },
  303.  
  304.     { IPT_AD_STICK_Y | IPF_PLAYER1, "AD Stick Y",   SEQ_DEF_3(KEYCODE_UP, CODE_OR, JOYCODE_1_UP) },
  305.     { (IPT_AD_STICK_Y | IPF_PLAYER1)+IPT_EXTENSION,                "AD Stick Y",   SEQ_DEF_3(KEYCODE_DOWN, CODE_OR, JOYCODE_1_DOWN) },
  306.     { IPT_AD_STICK_Y | IPF_PLAYER2, "AD Stick Y 2", SEQ_DEF_3(KEYCODE_R, CODE_OR, JOYCODE_2_UP) },
  307.     { (IPT_AD_STICK_Y | IPF_PLAYER2)+IPT_EXTENSION,                "AD Stick Y 2", SEQ_DEF_3(KEYCODE_F, CODE_OR, JOYCODE_2_DOWN) },
  308.     { IPT_AD_STICK_Y | IPF_PLAYER3, "AD Stick Y 3", SEQ_DEF_3(KEYCODE_I, CODE_OR, JOYCODE_3_UP) },
  309.     { (IPT_AD_STICK_Y | IPF_PLAYER3)+IPT_EXTENSION,                "AD Stick Y 3", SEQ_DEF_3(KEYCODE_K, CODE_OR, JOYCODE_3_DOWN) },
  310.     { IPT_AD_STICK_Y | IPF_PLAYER4, "AD Stick Y 4", SEQ_DEF_1(JOYCODE_4_UP) },
  311.     { (IPT_AD_STICK_Y | IPF_PLAYER4)+IPT_EXTENSION,                "AD Stick Y 4", SEQ_DEF_1(JOYCODE_4_DOWN) },
  312.  
  313.     { IPT_UNKNOWN,             "UNKNOWN",         SEQ_DEF_0 },
  314.     { IPT_END,                 0,                 SEQ_DEF_0 }    /* returned when there is no match */
  315. };
  316.  
  317. struct ipd inputport_defaults_backup[sizeof(inputport_defaults)/sizeof(struct ipd)];
  318.  
  319. /***************************************************************************/
  320. /* Generic IO */
  321.  
  322. static int readint(void *f,UINT32 *num)
  323. {
  324.     unsigned i;
  325.  
  326.     *num = 0;
  327.     for (i = 0;i < sizeof(UINT32);i++)
  328.     {
  329.         unsigned char c;
  330.  
  331.  
  332.         *num <<= 8;
  333.         if (osd_fread(f,&c,1) != 1)
  334.             return -1;
  335.         *num |= c;
  336.     }
  337.  
  338.     return 0;
  339. }
  340.  
  341. static void writeint(void *f,UINT32 num)
  342. {
  343.     unsigned i;
  344.  
  345.     for (i = 0;i < sizeof(UINT32);i++)
  346.     {
  347.         unsigned char c;
  348.  
  349.  
  350.         c = (num >> 8 * (sizeof(UINT32)-1)) & 0xff;
  351.         osd_fwrite(f,&c,1);
  352.         num <<= 8;
  353.     }
  354. }
  355.  
  356. static int readword(void *f,UINT16 *num)
  357. {
  358.     unsigned i;
  359.     int res;
  360.  
  361.     res = 0;
  362.     for (i = 0;i < sizeof(UINT16);i++)
  363.     {
  364.         unsigned char c;
  365.  
  366.  
  367.         res <<= 8;
  368.         if (osd_fread(f,&c,1) != 1)
  369.             return -1;
  370.         res |= c;
  371.     }
  372.  
  373.     *num = res;
  374.     return 0;
  375. }
  376.  
  377. static void writeword(void *f,UINT16 num)
  378. {
  379.     unsigned i;
  380.  
  381.     for (i = 0;i < sizeof(UINT16);i++)
  382.     {
  383.         unsigned char c;
  384.  
  385.  
  386.         c = (num >> 8 * (sizeof(UINT16)-1)) & 0xff;
  387.         osd_fwrite(f,&c,1);
  388.         num <<= 8;
  389.     }
  390. }
  391.  
  392. #ifndef NOLEGACY
  393. #include "legacy.h"
  394. #endif
  395.  
  396. static int seq_read_ver_8(void* f, InputSeq* seq)
  397. {
  398.     int j,len;
  399.     UINT32 i;
  400.     UINT16 w;
  401.  
  402.     if (readword(f,&w) != 0)
  403.         return -1;
  404.  
  405.     len = w;
  406.     seq_set_0(seq);
  407.     for(j=0;j<len;++j)
  408.     {
  409.         if (readint(f,&i) != 0)
  410.              return -1;
  411.         (*seq)[j] = savecode_to_code(i);
  412.      }
  413.  
  414.      return 0;
  415.   }
  416.  
  417. static int seq_read(void* f, InputSeq* seq, int ver)
  418.   {
  419. #ifdef NOLEGACY
  420.     if (ver==8)
  421.         return seq_read_ver_8(f,seq);
  422. #else
  423.     switch (ver) {
  424.         case 5 : return seq_read_ver_5(f,seq);
  425.         case 6 : return seq_read_ver_6(f,seq);
  426.         case 7 : return seq_read_ver_7(f,seq);
  427.         case 8 : return seq_read_ver_8(f,seq);
  428.     }
  429. #endif
  430.     return -1;
  431.   }
  432.  
  433. static void seq_write(void* f, InputSeq* seq)
  434.   {
  435.     int j,len;
  436.         for(len=0;len<SEQ_MAX;++len)
  437.         if ((*seq)[len] == CODE_NONE)
  438.             break;
  439.     writeword(f,len);
  440.     for(j=0;j<len;++j)
  441.         writeint(f, code_to_savecode( (*seq)[j] ));
  442.   }
  443.  
  444. /***************************************************************************/
  445. /* Load */
  446.  
  447. static void load_default_keys(void)
  448. {
  449.     void *f;
  450.  
  451.  
  452.     osd_customize_inputport_defaults(inputport_defaults);
  453.     memcpy(inputport_defaults_backup,inputport_defaults,sizeof(inputport_defaults));
  454.  
  455.     if ((f = osd_fopen("default",0,OSD_FILETYPE_CONFIG,0)) != 0)
  456.     {
  457.         char buf[8];
  458.         int version;
  459.  
  460.         /* read header */
  461.         if (osd_fread(f,buf,8) != 8)
  462.             goto getout;
  463.  
  464.         if (memcmp(buf,MAMEDEFSTRING_V5,8) == 0)
  465.             version = 5;
  466.         else if (memcmp(buf,MAMEDEFSTRING_V6,8) == 0)
  467.             version = 6;
  468.         else if (memcmp(buf,MAMEDEFSTRING_V7,8) == 0)
  469.             version = 7;
  470.         else if (memcmp(buf,MAMEDEFSTRING_V8,8) == 0)
  471.             version = 8;
  472.         else
  473.             goto getout;    /* header invalid */
  474.  
  475.         for (;;)
  476.         {
  477.             UINT32 type;
  478.             InputSeq def_seq;
  479.             InputSeq seq;
  480.             int i;
  481.  
  482.             if (readint(f,&type) != 0)
  483.                 goto getout;
  484.  
  485.             if (seq_read(f,&def_seq,version)!=0)
  486.                 goto getout;
  487.             if (seq_read(f,&seq,version)!=0)
  488.                 goto getout;
  489.  
  490.             i = 0;
  491.             while (inputport_defaults[i].type != IPT_END)
  492.             {
  493.                 if (inputport_defaults[i].type == type)
  494.                 {
  495.                     /* load stored settings only if the default hasn't changed */
  496.                     if (seq_cmp(&inputport_defaults[i].seq,&def_seq)==0)
  497.                         seq_copy(&inputport_defaults[i].seq,&seq);
  498.                 }
  499.  
  500.                 i++;
  501.             }
  502.         }
  503.  
  504. getout:
  505.         osd_fclose(f);
  506.     }
  507. }
  508.  
  509. static void save_default_keys(void)
  510. {
  511.     void *f;
  512.  
  513.  
  514.     if ((f = osd_fopen("default",0,OSD_FILETYPE_CONFIG,1)) != 0)
  515.     {
  516.         int i;
  517.  
  518.  
  519.         /* write header */
  520.         osd_fwrite(f,MAMEDEFSTRING_V8,8);
  521.  
  522.         i = 0;
  523.         while (inputport_defaults[i].type != IPT_END)
  524.         {
  525.             writeint(f,inputport_defaults[i].type);
  526.  
  527.             seq_write(f,&inputport_defaults_backup[i].seq);
  528.             seq_write(f,&inputport_defaults[i].seq);
  529.  
  530.             i++;
  531.         }
  532.  
  533.         osd_fclose(f);
  534.     }
  535.     memcpy(inputport_defaults,inputport_defaults_backup,sizeof(inputport_defaults_backup));
  536. }
  537.  
  538. static int input_port_read_ver_8(void *f,struct InputPort *in)
  539. {
  540.     UINT32 i;
  541.     UINT16 w;
  542.     if (readint(f,&i) != 0)
  543.         return -1;
  544.     in->type = i;
  545.  
  546.     if (readword(f,&w) != 0)
  547.         return -1;
  548.     in->mask = w;
  549.  
  550.     if (readword(f,&w) != 0)
  551.         return -1;
  552.     in->default_value = w;
  553.  
  554.     if (seq_read_ver_8(f,&in->seq) != 0)
  555.         return -1;
  556.  
  557.     return 0;
  558. }
  559.  
  560. static int input_port_read(void *f,struct InputPort *in, int ver)
  561. {
  562. #ifdef NOLEGACY
  563.     if (ver==8)
  564.         return input_port_read_ver_8(f,in);
  565. #else
  566.     switch (ver) {
  567.         case 5 : return    input_port_read_ver_5(f,in);
  568.         case 6 : return    input_port_read_ver_6(f,in);
  569.         case 7 : return    input_port_read_ver_7(f,in);
  570.         case 8 : return    input_port_read_ver_8(f,in);
  571.     }
  572. #endif
  573.     return -1;
  574. }
  575.  
  576. static void input_port_write(void *f,struct InputPort *in)
  577. {
  578.     writeint(f,in->type);
  579.     writeword(f,in->mask);
  580.     writeword(f,in->default_value);
  581.     seq_write(f,&in->seq);
  582. }
  583.  
  584.  
  585. int load_input_port_settings(void)
  586. {
  587.     void *f;
  588. #ifdef MAME_NET
  589.     struct InputPort *in;
  590.     int port, player;
  591. #endif /* MAME_NET */
  592.  
  593.  
  594.     load_default_keys();
  595.  
  596.     if ((f = osd_fopen(Machine->gamedrv->name,0,OSD_FILETYPE_CONFIG,0)) != 0)
  597.     {
  598. #ifndef MAME_NET
  599.         struct InputPort *in;
  600. #endif
  601.         unsigned int total,savedtotal;
  602.         char buf[8];
  603.         int i;
  604.         int version;
  605.  
  606.         in = Machine->input_ports_default;
  607.  
  608.         /* calculate the size of the array */
  609.         total = 0;
  610.         while (in->type != IPT_END)
  611.         {
  612.             total++;
  613.             in++;
  614.         }
  615.  
  616.         /* read header */
  617.         if (osd_fread(f,buf,8) != 8)
  618.             goto getout;
  619.  
  620.         if (memcmp(buf,MAMECFGSTRING_V5,8) == 0)
  621.             version = 5;
  622.         else if (memcmp(buf,MAMECFGSTRING_V6,8) == 0)
  623.             version = 6;
  624.         else if (memcmp(buf,MAMECFGSTRING_V7,8) == 0)
  625.             version = 7;
  626.         else if (memcmp(buf,MAMECFGSTRING_V8,8) == 0)
  627.             version = 8;
  628.         else
  629.             goto getout;    /* header invalid */
  630.  
  631.         /* read array size */
  632.         if (readint(f,&savedtotal) != 0)
  633.             goto getout;
  634.         if (total != savedtotal)
  635.             goto getout;    /* different size */
  636.  
  637.         /* read the original settings and compare them with the ones defined in the driver */
  638.         in = Machine->input_ports_default;
  639.         while (in->type != IPT_END)
  640.         {
  641.             struct InputPort saved;
  642.  
  643.             if (input_port_read(f,&saved,version) != 0)
  644.                 goto getout;
  645.  
  646.             if (in->mask != saved.mask ||
  647.                 in->default_value != saved.default_value ||
  648.                 in->type != saved.type ||
  649.                 seq_cmp(&in->seq,&saved.seq) !=0 )
  650.             goto getout;    /* the default values are different */
  651.  
  652.             in++;
  653.         }
  654.  
  655.         /* read the current settings */
  656.         in = Machine->input_ports;
  657.         while (in->type != IPT_END)
  658.         {
  659.             if (input_port_read(f,in,version) != 0)
  660.                 goto getout;
  661.             in++;
  662.         }
  663.  
  664.         /* Clear the coin & ticket counters/flags - LBO 042898 */
  665.         for (i = 0; i < COIN_COUNTERS; i ++)
  666.             coins[i] = lastcoin[i] = coinlockedout[i] = 0;
  667.         dispensed_tickets = 0;
  668.  
  669.         /* read in the coin/ticket counters */
  670.         for (i = 0; i < COIN_COUNTERS; i ++)
  671.         {
  672.             if (readint(f,&coins[i]) != 0)
  673.                 goto getout;
  674.         }
  675.         if (readint(f,&dispensed_tickets) != 0)
  676.             goto getout;
  677.  
  678.         mixer_read_config(f);
  679.  
  680. getout:
  681.         osd_fclose(f);
  682.     }
  683.  
  684.     /* All analog ports need initialization */
  685.     {
  686.         int i;
  687.         for (i = 0; i < MAX_INPUT_PORTS; i++)
  688.             input_analog_init[i] = 1;
  689.     }
  690. #ifdef MAME_NET
  691.     /* Find out what port is used by what player and swap regular inputs */
  692.     in = Machine->input_ports;
  693.  
  694. //    if (in->type == IPT_END) return;     /* nothing to do */
  695.  
  696.     /* make sure the InputPort definition is correct */
  697. //    if (in->type != IPT_PORT)
  698. //    {
  699. //        logerror("Error in InputPort definition: expecting PORT_START\n");
  700. //        return;
  701. //    }
  702. //    else in++;
  703.     in++;
  704.  
  705.     /* scan all the input ports */
  706.     port = 0;
  707.     while (in->type != IPT_END && port < MAX_INPUT_PORTS)
  708.     {
  709.         /* now check the input bits. */
  710.         while (in->type != IPT_END && in->type != IPT_PORT)
  711.         {
  712.             if ((in->type & ~IPF_MASK) != IPT_DIPSWITCH_SETTING &&    /* skip dipswitch definitions */
  713.                 (in->type & ~IPF_MASK) != IPT_EXTENSION &&            /* skip analog extension fields */
  714.                 (in->type & IPF_UNUSED) == 0 &&                        /* skip unused bits */
  715.                 !(!options.cheat && (in->type & IPF_CHEAT)) &&                /* skip cheats if cheats disabled */
  716.                 (in->type & ~IPF_MASK) != IPT_VBLANK &&                /* skip vblank stuff */
  717.                 ((in->type & ~IPF_MASK) >= IPT_COIN1 &&                /* skip if coin input and it's locked out */
  718.                 (in->type & ~IPF_MASK) <= IPT_COIN4 &&
  719.                  coinlockedout[(in->type & ~IPF_MASK) - IPT_COIN1]))
  720.             {
  721.                 player = 0;
  722.                 if ((in->type & IPF_PLAYERMASK) == IPF_PLAYER2) player = 1;
  723.                 else if ((in->type & IPF_PLAYERMASK) == IPF_PLAYER3) player = 2;
  724.                 else if ((in->type & IPF_PLAYERMASK) == IPF_PLAYER4) player = 3;
  725.  
  726.                 if (((in->type & ~IPF_MASK) > IPT_ANALOG_START)
  727.                     && ((in->type & ~IPF_MASK) < IPT_ANALOG_END))
  728.                 {
  729.                     analog_player_port[port] = player;
  730.                 }
  731.                 if (((in->type & ~IPF_MASK) == IPT_BUTTON1) ||
  732.                     ((in->type & ~IPF_MASK) == IPT_BUTTON2) ||
  733.                     ((in->type & ~IPF_MASK) == IPT_BUTTON3) ||
  734.                     ((in->type & ~IPF_MASK) == IPT_BUTTON4) ||
  735.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICK_UP) ||
  736.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICK_DOWN) ||
  737.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICK_LEFT) ||
  738.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICK_RIGHT) ||
  739.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICKRIGHT_UP) ||
  740.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICKRIGHT_DOWN) ||
  741.                      ((in->type & ~IPF_MASK) == IPT_JOYSTICKRIGHT_LEFT) ||
  742.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICKRIGHT_RIGHT) ||
  743.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICKLEFT_UP) ||
  744.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICKLEFT_DOWN) ||
  745.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICKLEFT_LEFT) ||
  746.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICKLEFT_RIGHT) ||
  747.                     ((in->type & ~IPF_MASK) == IPT_PADDLE) ||
  748.                     ((in->type & ~IPF_MASK) == IPT_DIAL) ||
  749.                     ((in->type & ~IPF_MASK) == IPT_TRACKBALL_X) ||
  750.                     ((in->type & ~IPF_MASK) == IPT_TRACKBALL_Y) ||
  751.                     ((in->type & ~IPF_MASK) == IPT_AD_STICK_X) ||
  752.                     ((in->type & ~IPF_MASK) == IPT_AD_STICK_Y))
  753.                 {
  754.                     switch (default_player)
  755.                     {
  756.                         case 0:
  757.                             /* do nothing */
  758.                             break;
  759.                         case 1:
  760.                             if (player == 0)
  761.                             {
  762.                                 in->type &= ~IPF_PLAYER1;
  763.                                 in->type |= IPF_PLAYER2;
  764.                             }
  765.                             else if (player == 1)
  766.                             {
  767.                                 in->type &= ~IPF_PLAYER2;
  768.                                 in->type |= IPF_PLAYER1;
  769.                             }
  770.                             break;
  771.                         case 2:
  772.                             if (player == 0)
  773.                             {
  774.                                 in->type &= ~IPF_PLAYER1;
  775.                                 in->type |= IPF_PLAYER3;
  776.                             }
  777.                             else if (player == 2)
  778.                             {
  779.                                 in->type &= ~IPF_PLAYER3;
  780.                                 in->type |= IPF_PLAYER1;
  781.                             }
  782.                             break;
  783.                         case 3:
  784.                             if (player == 0)
  785.                             {
  786.                                 in->type &= ~IPF_PLAYER1;
  787.                                 in->type |= IPF_PLAYER4;
  788.                             }
  789.                             else if (player == 3)
  790.                             {
  791.                                 in->type &= ~IPF_PLAYER4;
  792.                                 in->type |= IPF_PLAYER1;
  793.                             }
  794.                             break;
  795.                     }
  796.                 }
  797.             }
  798.             in++;
  799.         }
  800.         port++;
  801.         if (in->type == IPT_PORT) in++;
  802.     }
  803.  
  804.     /* TODO: at this point the games should initialize peers to same as server */
  805.  
  806. #endif /* MAME_NET */
  807.  
  808.     update_input_ports();
  809.  
  810.     /* if we didn't find a saved config, return 0 so the main core knows that it */
  811.     /* is the first time the game is run and it should diplay the disclaimer. */
  812.     if (f) return 1;
  813.     else return 0;
  814. }
  815.  
  816. /***************************************************************************/
  817. /* Save */
  818.  
  819. void save_input_port_settings(void)
  820. {
  821.     void *f;
  822. #ifdef MAME_NET
  823.     struct InputPort *in;
  824.     int port, player;
  825.  
  826.     /* Swap input port definitions back to defaults */
  827.     in = Machine->input_ports;
  828.  
  829.     if (in->type == IPT_END) return;     /* nothing to do */
  830.  
  831.     /* make sure the InputPort definition is correct */
  832.     if (in->type != IPT_PORT)
  833.     {
  834.         logerror("Error in InputPort definition: expecting PORT_START\n");
  835.         return;
  836.     }
  837.     else in++;
  838.  
  839.     /* scan all the input ports */
  840.     port = 0;
  841.     while (in->type != IPT_END && port < MAX_INPUT_PORTS)
  842.     {
  843.         /* now check the input bits. */
  844.         while (in->type != IPT_END && in->type != IPT_PORT)
  845.         {
  846.             if ((in->type & ~IPF_MASK) != IPT_DIPSWITCH_SETTING &&    /* skip dipswitch definitions */
  847.                 (in->type & ~IPF_MASK) != IPT_EXTENSION &&            /* skip analog extension fields */
  848.                 (in->type & IPF_UNUSED) == 0 &&                        /* skip unused bits */
  849.                 !(!options.cheat && (in->type & IPF_CHEAT)) &&                /* skip cheats if cheats disabled */
  850.                 (in->type & ~IPF_MASK) != IPT_VBLANK &&                /* skip vblank stuff */
  851.                 ((in->type & ~IPF_MASK) >= IPT_COIN1 &&                /* skip if coin input and it's locked out */
  852.                 (in->type & ~IPF_MASK) <= IPT_COIN4 &&
  853.                  coinlockedout[(in->type & ~IPF_MASK) - IPT_COIN1]))
  854.             {
  855.                 player = 0;
  856.                 if ((in->type & IPF_PLAYERMASK) == IPF_PLAYER2) player = 1;
  857.                 else if ((in->type & IPF_PLAYERMASK) == IPF_PLAYER3) player = 2;
  858.                 else if ((in->type & IPF_PLAYERMASK) == IPF_PLAYER4) player = 3;
  859.  
  860.                 if (((in->type & ~IPF_MASK) == IPT_BUTTON1) ||
  861.                     ((in->type & ~IPF_MASK) == IPT_BUTTON2) ||
  862.                     ((in->type & ~IPF_MASK) == IPT_BUTTON3) ||
  863.                     ((in->type & ~IPF_MASK) == IPT_BUTTON4) ||
  864.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICK_UP) ||
  865.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICK_DOWN) ||
  866.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICK_LEFT) ||
  867.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICK_RIGHT) ||
  868.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICKRIGHT_UP) ||
  869.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICKRIGHT_DOWN) ||
  870.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICKRIGHT_LEFT) ||
  871.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICKRIGHT_RIGHT) ||
  872.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICKLEFT_UP) ||
  873.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICKLEFT_DOWN) ||
  874.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICKLEFT_LEFT) ||
  875.                     ((in->type & ~IPF_MASK) == IPT_JOYSTICKLEFT_RIGHT) ||
  876.                     ((in->type & ~IPF_MASK) == IPT_PADDLE) ||
  877.                     ((in->type & ~IPF_MASK) == IPT_DIAL) ||
  878.                     ((in->type & ~IPF_MASK) == IPT_TRACKBALL_X) ||
  879.                     ((in->type & ~IPF_MASK) == IPT_TRACKBALL_Y) ||
  880.                     ((in->type & ~IPF_MASK) == IPT_AD_STICK_X) ||
  881.                     ((in->type & ~IPF_MASK) == IPT_AD_STICK_Y))
  882.                 {
  883.                     switch (default_player)
  884.                     {
  885.                         case 0:
  886.                             /* do nothing */
  887.                             analog_player_port[port] = player;
  888.                             break;
  889.                         case 1:
  890.                             if (player == 0)
  891.                             {
  892.                                 in->type &= ~IPF_PLAYER1;
  893.                                 in->type |= IPF_PLAYER2;
  894.                                 analog_player_port[port] = 1;
  895.                             }
  896.                             else if (player == 1)
  897.                             {
  898.                                 in->type &= ~IPF_PLAYER2;
  899.                                 in->type |= IPF_PLAYER1;
  900.                                 analog_player_port[port] = 0;
  901.                             }
  902.                             break;
  903.                         case 2:
  904.                             if (player == 0)
  905.                             {
  906.                                 in->type &= ~IPF_PLAYER1;
  907.                                 in->type |= IPF_PLAYER3;
  908.                                 analog_player_port[port] = 2;
  909.                             }
  910.                             else if (player == 2)
  911.                             {
  912.                                 in->type &= ~IPF_PLAYER3;
  913.                                 in->type |= IPF_PLAYER1;
  914.                                 analog_player_port[port] = 0;
  915.                             }
  916.                             break;
  917.                         case 3:
  918.                             if (player == 0)
  919.                             {
  920.                                 in->type &= ~IPF_PLAYER1;
  921.                                 in->type |= IPF_PLAYER4;
  922.                                 analog_player_port[port] = 3;
  923.                             }
  924.                             else if (player == 3)
  925.                             {
  926.                                 in->type &= ~IPF_PLAYER4;
  927.                                 in->type |= IPF_PLAYER1;
  928.                                 analog_player_port[port] = 0;
  929.                             }
  930.                             break;
  931.                     }
  932.                 }
  933.             }
  934.             in++;
  935.         }
  936.         port++;
  937.         if (in->type == IPT_PORT) in++;
  938.     }
  939. #endif /* MAME_NET */
  940.  
  941.     save_default_keys();
  942.  
  943.     if ((f = osd_fopen(Machine->gamedrv->name,0,OSD_FILETYPE_CONFIG,1)) != 0)
  944.     {
  945. #ifndef MAME_NET
  946.         struct InputPort *in;
  947. #endif /* MAME_NET */
  948.         int total;
  949.         int i;
  950.  
  951.  
  952.         in = Machine->input_ports_default;
  953.  
  954.         /* calculate the size of the array */
  955.         total = 0;
  956.         while (in->type != IPT_END)
  957.         {
  958.             total++;
  959.             in++;
  960.         }
  961.  
  962.         /* write header */
  963.         osd_fwrite(f,MAMECFGSTRING_V8,8);
  964.         /* write array size */
  965.         writeint(f,total);
  966.         /* write the original settings as defined in the driver */
  967.         in = Machine->input_ports_default;
  968.         while (in->type != IPT_END)
  969.         {
  970.             input_port_write(f,in);
  971.             in++;
  972.         }
  973.         /* write the current settings */
  974.         in = Machine->input_ports;
  975.         while (in->type != IPT_END)
  976.         {
  977.             input_port_write(f,in);
  978.             in++;
  979.         }
  980.  
  981.         /* write out the coin/ticket counters for this machine - LBO 042898 */
  982.         for (i = 0; i < COIN_COUNTERS; i ++)
  983.             writeint(f,coins[i]);
  984.         writeint(f,dispensed_tickets);
  985.  
  986.         mixer_write_config(f);
  987.  
  988.         osd_fclose(f);
  989.     }
  990. }
  991.  
  992.  
  993.  
  994. /* Note that the following 3 routines have slightly different meanings with analog ports */
  995. const char *input_port_name(const struct InputPort *in)
  996. {
  997.     int i;
  998.     unsigned type;
  999.  
  1000.     if (in->name != IP_NAME_DEFAULT) return in->name;
  1001.  
  1002.     i = 0;
  1003.  
  1004.     if ((in->type & ~IPF_MASK) == IPT_EXTENSION)
  1005.         type = (in-1)->type & (~IPF_MASK | IPF_PLAYERMASK);
  1006.     else
  1007.         type = in->type & (~IPF_MASK | IPF_PLAYERMASK);
  1008.  
  1009.     while (inputport_defaults[i].type != IPT_END &&
  1010.             inputport_defaults[i].type != type)
  1011.         i++;
  1012.  
  1013.     if ((in->type & ~IPF_MASK) == IPT_EXTENSION)
  1014.         return inputport_defaults[i+1].name;
  1015.     else
  1016.         return inputport_defaults[i].name;
  1017. }
  1018.  
  1019. InputSeq* input_port_type_seq(int type)
  1020. {
  1021.     unsigned i;
  1022.  
  1023.     i = 0;
  1024.  
  1025.     while (inputport_defaults[i].type != IPT_END &&
  1026.             inputport_defaults[i].type != type)
  1027.         i++;
  1028.  
  1029.     return &inputport_defaults[i].seq;
  1030. }
  1031.  
  1032. InputSeq* input_port_seq(const struct InputPort *in)
  1033. {
  1034.     int i,type;
  1035.  
  1036.     static InputSeq ip_none = SEQ_DEF_1(CODE_NONE);
  1037.  
  1038.     while (seq_get_1((InputSeq*)&in->seq) == CODE_PREVIOUS) in--;
  1039.  
  1040.     if ((in->type & ~IPF_MASK) == IPT_EXTENSION)
  1041.     {
  1042.         type = (in-1)->type & (~IPF_MASK | IPF_PLAYERMASK);
  1043.         /* if port is disabled, or cheat with cheats disabled, return no key */
  1044.         if (((in-1)->type & IPF_UNUSED) || (!options.cheat && ((in-1)->type & IPF_CHEAT)))
  1045.             return &ip_none;
  1046.     }
  1047.     else
  1048.     {
  1049.         type = in->type & (~IPF_MASK | IPF_PLAYERMASK);
  1050.         /* if port is disabled, or cheat with cheats disabled, return no key */
  1051.         if ((in->type & IPF_UNUSED) || (!options.cheat && (in->type & IPF_CHEAT)))
  1052.             return &ip_none;
  1053.     }
  1054.  
  1055.     if (seq_get_1((InputSeq*)&in->seq) != CODE_DEFAULT)
  1056.         return (InputSeq*)&in->seq;
  1057.  
  1058.     i = 0;
  1059.  
  1060.     while (inputport_defaults[i].type != IPT_END &&
  1061.             inputport_defaults[i].type != type)
  1062.         i++;
  1063.  
  1064.     if ((in->type & ~IPF_MASK) == IPT_EXTENSION)
  1065.         return &inputport_defaults[i+1].seq;
  1066.     else
  1067.         return &inputport_defaults[i].seq;
  1068. }
  1069.  
  1070. void update_analog_port(int port)
  1071. {
  1072.     struct InputPort *in;
  1073.     int current, delta, type, sensitivity, min, max, default_value;
  1074.     int axis, is_stick, check_bounds;
  1075.     InputSeq* incseq;
  1076.     InputSeq* decseq;
  1077.     int keydelta;
  1078.     int player;
  1079.  
  1080.     /* get input definition */
  1081.     in = input_analog[port];
  1082.  
  1083.     /* if we're not cheating and this is a cheat-only port, bail */
  1084.     if (!options.cheat && (in->type & IPF_CHEAT)) return;
  1085.     type=(in->type & ~IPF_MASK);
  1086.  
  1087.     decseq = input_port_seq(in);
  1088.     incseq = input_port_seq(in+1);
  1089.  
  1090.     keydelta = IP_GET_DELTA(in);
  1091.  
  1092.     switch (type)
  1093.     {
  1094.         case IPT_PADDLE:
  1095.             axis = X_AXIS; is_stick = 0; check_bounds = 1; break;
  1096.         case IPT_PADDLE_V:
  1097.             axis = Y_AXIS; is_stick = 0; check_bounds = 1; break;
  1098.         case IPT_DIAL:
  1099.             axis = X_AXIS; is_stick = 0; check_bounds = 0; break;
  1100.         case IPT_DIAL_V:
  1101.             axis = Y_AXIS; is_stick = 0; check_bounds = 0; break;
  1102.         case IPT_TRACKBALL_X:
  1103.             axis = X_AXIS; is_stick = 0; check_bounds = 0; break;
  1104.         case IPT_TRACKBALL_Y:
  1105.             axis = Y_AXIS; is_stick = 0; check_bounds = 0; break;
  1106.         case IPT_AD_STICK_X:
  1107.             axis = X_AXIS; is_stick = 1; check_bounds = 1; break;
  1108.         case IPT_AD_STICK_Y:
  1109.             axis = Y_AXIS; is_stick = 1; check_bounds = 1; break;
  1110.         case IPT_PEDAL:
  1111.             axis = Y_AXIS; is_stick = 0; check_bounds = 1; break;
  1112.         default:
  1113.             /* Use some defaults to prevent crash */
  1114.             axis = X_AXIS; is_stick = 0; check_bounds = 0;
  1115.             logerror("Oops, polling non analog device in update_analog_port()????\n");
  1116.     }
  1117.  
  1118.  
  1119.     sensitivity = IP_GET_SENSITIVITY(in);
  1120.     min = IP_GET_MIN(in);
  1121.     max = IP_GET_MAX(in);
  1122.     default_value = in->default_value * 100 / sensitivity;
  1123.     /* extremes can be either signed or unsigned */
  1124.     if (min > max)
  1125.     {
  1126.         if (in->mask > 0xff) min = min - 0x10000;
  1127.         else min = min - 0x100;
  1128.     }
  1129.  
  1130.  
  1131.     input_analog_previous_value[port] = input_analog_current_value[port];
  1132.  
  1133.     /* if IPF_CENTER go back to the default position */
  1134.     /* sticks are handled later... */
  1135.     if ((in->type & IPF_CENTER) && (!is_stick))
  1136.         input_analog_current_value[port] = in->default_value * 100 / sensitivity;
  1137.  
  1138.     current = input_analog_current_value[port];
  1139.  
  1140.     delta = 0;
  1141.  
  1142.     switch (in->type & IPF_PLAYERMASK)
  1143.     {
  1144.         case IPF_PLAYER2:          player = 1; break;
  1145.         case IPF_PLAYER3:          player = 2; break;
  1146.         case IPF_PLAYER4:          player = 3; break;
  1147.         case IPF_PLAYER1: default: player = 0; break;
  1148.     }
  1149.  
  1150.     if (axis == X_AXIS)
  1151.         delta = mouse_delta_x[player];
  1152.     else
  1153.         delta = mouse_delta_y[player];
  1154.  
  1155.     if (seq_pressed(decseq)) delta -= keydelta;
  1156.  
  1157.     if (type != IPT_PEDAL)
  1158.     {
  1159.         if (seq_pressed(incseq)) delta += keydelta;
  1160.     }
  1161.     else
  1162.     {
  1163.         /* is this cheesy or what? */
  1164.         if (!delta && seq_get_1(incseq) == KEYCODE_Y) delta += keydelta;
  1165.         delta = -delta;
  1166.     }
  1167.  
  1168.     if (in->type & IPF_REVERSE) delta = -delta;
  1169.  
  1170.     if (is_stick)
  1171.     {
  1172.         int new, prev;
  1173.  
  1174.         /* center stick */
  1175.         if ((delta == 0) && (in->type & IPF_CENTER))
  1176.         {
  1177.             if (current > default_value)
  1178.             delta = -100 / sensitivity;
  1179.             if (current < default_value)
  1180.             delta = 100 / sensitivity;
  1181.         }
  1182.  
  1183.         /* An analog joystick which is not at zero position (or has just */
  1184.         /* moved there) takes precedence over all other computations */
  1185.         /* analog_x/y holds values from -128 to 128 (yes, 128, not 127) */
  1186.  
  1187.         if (axis == X_AXIS)
  1188.         {
  1189.             new  = analog_current_x[player];
  1190.             prev = analog_previous_x[player];
  1191.         }
  1192.         else
  1193.         {
  1194.             new  = analog_current_y[player];
  1195.             prev = analog_previous_y[player];
  1196.         }
  1197.  
  1198.         if ((new != 0) || (new-prev != 0))
  1199.         {
  1200.             delta=0;
  1201.  
  1202.             if (in->type & IPF_REVERSE)
  1203.             {
  1204.                 new  = -new;
  1205.                 prev = -prev;
  1206.             }
  1207.  
  1208.             /* apply sensitivity using a logarithmic scale */
  1209.             if (in->mask > 0xff)
  1210.             {
  1211.                 if (new > 0)
  1212.                 {
  1213.                     current = (pow(new / 32768.0, 100.0 / sensitivity) * (max-in->default_value)
  1214.                             + in->default_value) * 100 / sensitivity;
  1215.                 }
  1216.                 else
  1217.                 {
  1218.                     current = (pow(-new / 32768.0, 100.0 / sensitivity) * (min-in->default_value)
  1219.                             + in->default_value) * 100 / sensitivity;
  1220.                 }
  1221.             }
  1222.             else
  1223.             {
  1224.                 if (new > 0)
  1225.                 {
  1226.                     current = (pow(new / 128.0, 100.0 / sensitivity) * (max-in->default_value)
  1227.                             + in->default_value) * 100 / sensitivity;
  1228.                 }
  1229.                 else
  1230.                 {
  1231.                     current = (pow(-new / 128.0, 100.0 / sensitivity) * (min-in->default_value)
  1232.                             + in->default_value) * 100 / sensitivity;
  1233.                 }
  1234.             }
  1235.         }
  1236.     }
  1237.  
  1238.     current += delta;
  1239.  
  1240.     if (check_bounds)
  1241.     {
  1242.         if ((current * sensitivity + 50) / 100 < min)
  1243.             current = (min * 100 + sensitivity/2) / sensitivity;
  1244.         if ((current * sensitivity + 50) / 100 > max)
  1245.             current = (max * 100 + sensitivity/2) / sensitivity;
  1246.     }
  1247.  
  1248.     input_analog_current_value[port] = current;
  1249. }
  1250.  
  1251. static void scale_analog_port(int port)
  1252. {
  1253.     struct InputPort *in;
  1254.     int delta,current,sensitivity;
  1255.  
  1256. profiler_mark(PROFILER_INPUT);
  1257.     in = input_analog[port];
  1258.     sensitivity = IP_GET_SENSITIVITY(in);
  1259.  
  1260.     delta = cpu_scalebyfcount(input_analog_current_value[port] - input_analog_previous_value[port]);
  1261.  
  1262.     current = input_analog_previous_value[port] + delta;
  1263.  
  1264.     input_port_value[port] &= ~in->mask;
  1265.     input_port_value[port] |= ((current * sensitivity + 50) / 100) & in->mask;
  1266.  
  1267.     if (playback)
  1268.         readword(playback,&input_port_value[port]);
  1269.     if (record)
  1270.         writeword(record,input_port_value[port]);
  1271. #ifdef MAME_NET
  1272.     if ( net_active() && (default_player != NET_SPECTATOR) )
  1273.         net_analog_sync((unsigned char *) input_port_value, port, analog_player_port, default_player);
  1274. #endif /* MAME_NET */
  1275. profiler_mark(PROFILER_END);
  1276. }
  1277.  
  1278.  
  1279. void update_input_ports(void)
  1280. {
  1281.     int port,ib;
  1282.     struct InputPort *in;
  1283. #define MAX_INPUT_BITS 1024
  1284. static int impulsecount[MAX_INPUT_BITS];
  1285. static int waspressed[MAX_INPUT_BITS];
  1286. #define MAX_JOYSTICKS 3
  1287. #define MAX_PLAYERS 4
  1288. #ifdef MRU_JOYSTICK
  1289. static int update_serial_number = 1;
  1290. static int joyserial[MAX_JOYSTICKS*MAX_PLAYERS][4];
  1291. #else
  1292. int joystick[MAX_JOYSTICKS*MAX_PLAYERS][4];
  1293. #endif
  1294.  
  1295. #ifdef MAME_NET
  1296. int player;
  1297. #endif /* MAME_NET */
  1298.  
  1299.  
  1300. profiler_mark(PROFILER_INPUT);
  1301.  
  1302.     /* clear all the values before proceeding */
  1303.     for (port = 0;port < MAX_INPUT_PORTS;port++)
  1304.     {
  1305.         input_port_value[port] = 0;
  1306.         input_vblank[port] = 0;
  1307.         input_analog[port] = 0;
  1308.     }
  1309.  
  1310. #ifndef MRU_JOYSTICK
  1311.     for (i = 0;i < 4*MAX_JOYSTICKS*MAX_PLAYERS;i++)
  1312.         joystick[i/4][i%4] = 0;
  1313. #endif
  1314.  
  1315.     in = Machine->input_ports;
  1316.  
  1317.     if (in->type == IPT_END) return;     /* nothing to do */
  1318.  
  1319.     /* make sure the InputPort definition is correct */
  1320.     if (in->type != IPT_PORT)
  1321.     {
  1322.         logerror("Error in InputPort definition: expecting PORT_START\n");
  1323.         return;
  1324.     }
  1325.     else in++;
  1326.  
  1327. #ifdef MRU_JOYSTICK
  1328.     /* scan all the joystick ports */
  1329.     port = 0;
  1330.     while (in->type != IPT_END && port < MAX_INPUT_PORTS)
  1331.     {
  1332.         while (in->type != IPT_END && in->type != IPT_PORT)
  1333.         {
  1334.             if ((in->type & ~IPF_MASK) >= IPT_JOYSTICK_UP &&
  1335.                 (in->type & ~IPF_MASK) <= IPT_JOYSTICKLEFT_RIGHT)
  1336.             {
  1337.                 InputSeq* seq;
  1338.  
  1339.                 seq = input_port_seq(in);
  1340.  
  1341.                 if (seq_get_1(seq) != 0 && seq_get_1(seq) != CODE_NONE)
  1342.                 {
  1343.                     int joynum,joydir,player;
  1344.  
  1345.                     player = 0;
  1346.                     if ((in->type & IPF_PLAYERMASK) == IPF_PLAYER2)
  1347.                         player = 1;
  1348.                     else if ((in->type & IPF_PLAYERMASK) == IPF_PLAYER3)
  1349.                         player = 2;
  1350.                     else if ((in->type & IPF_PLAYERMASK) == IPF_PLAYER4)
  1351.                         player = 3;
  1352.  
  1353.                     joynum = player * MAX_JOYSTICKS +
  1354.                              ((in->type & ~IPF_MASK) - IPT_JOYSTICK_UP) / 4;
  1355.                     joydir = ((in->type & ~IPF_MASK) - IPT_JOYSTICK_UP) % 4;
  1356.  
  1357.                     if (seq_pressed(seq))
  1358.                     {
  1359.                         if (joyserial[joynum][joydir] == 0)
  1360.                             joyserial[joynum][joydir] = update_serial_number;
  1361.                     }
  1362.                     else
  1363.                         joyserial[joynum][joydir] = 0;
  1364.                 }
  1365.             }
  1366.             in++;
  1367.         }
  1368.  
  1369.         port++;
  1370.         if (in->type == IPT_PORT) in++;
  1371.     }
  1372.     update_serial_number += 1;
  1373.  
  1374.     in = Machine->input_ports;
  1375.  
  1376.     /* already made sure the InputPort definition is correct */
  1377.     in++;
  1378. #endif
  1379.  
  1380.  
  1381.     /* scan all the input ports */
  1382.     port = 0;
  1383.     ib = 0;
  1384.     while (in->type != IPT_END && port < MAX_INPUT_PORTS)
  1385.     {
  1386.         struct InputPort *start;
  1387.  
  1388.  
  1389.         /* first of all, scan the whole input port definition and build the */
  1390.         /* default value. I must do it before checking for input because otherwise */
  1391.         /* multiple keys associated with the same input bit wouldn't work (the bit */
  1392.         /* would be reset to its default value by the second entry, regardless if */
  1393.         /* the key associated with the first entry was pressed) */
  1394.         start = in;
  1395.         while (in->type != IPT_END && in->type != IPT_PORT)
  1396.         {
  1397.             if ((in->type & ~IPF_MASK) != IPT_DIPSWITCH_SETTING &&    /* skip dipswitch definitions */
  1398.                 (in->type & ~IPF_MASK) != IPT_EXTENSION)            /* skip analog extension fields */
  1399.             {
  1400.                 input_port_value[port] =
  1401.                         (input_port_value[port] & ~in->mask) | (in->default_value & in->mask);
  1402. #ifdef MAME_NET
  1403.                 if ( net_active() )
  1404.                     input_port_defaults[port] = input_port_value[port];
  1405. #endif /* MAME_NET */
  1406.             }
  1407.  
  1408.             in++;
  1409.         }
  1410.  
  1411.         /* now get back to the beginning of the input port and check the input bits. */
  1412.         for (in = start;
  1413.              in->type != IPT_END && in->type != IPT_PORT;
  1414.              in++, ib++)
  1415.         {
  1416. #ifdef MAME_NET
  1417.             player = 0;
  1418.             if ((in->type & IPF_PLAYERMASK) == IPF_PLAYER2) player = 1;
  1419.             else if ((in->type & IPF_PLAYERMASK) == IPF_PLAYER3) player = 2;
  1420.             else if ((in->type & IPF_PLAYERMASK) == IPF_PLAYER4) player = 3;
  1421. #endif /* MAME_NET */
  1422.             if ((in->type & ~IPF_MASK) != IPT_DIPSWITCH_SETTING &&    /* skip dipswitch definitions */
  1423.                     (in->type & ~IPF_MASK) != IPT_EXTENSION)        /* skip analog extension fields */
  1424.             {
  1425.                 if ((in->type & ~IPF_MASK) == IPT_VBLANK)
  1426.                 {
  1427.                     input_vblank[port] ^= in->mask;
  1428.                     input_port_value[port] ^= in->mask;
  1429. if (Machine->drv->vblank_duration == 0)
  1430.     logerror("Warning: you are using IPT_VBLANK with vblank_duration = 0. You need to increase vblank_duration for IPT_VBLANK to work.\n");
  1431.                 }
  1432.                 /* If it's an analog control, handle it appropriately */
  1433.                 else if (((in->type & ~IPF_MASK) > IPT_ANALOG_START)
  1434.                       && ((in->type & ~IPF_MASK) < IPT_ANALOG_END  )) /* LBO 120897 */
  1435.                 {
  1436.                     input_analog[port]=in;
  1437.                     /* reset the analog port on first access */
  1438.                     if (input_analog_init[port])
  1439.                     {
  1440.                         input_analog_init[port] = 0;
  1441.                         input_analog_current_value[port] = input_analog_previous_value[port]
  1442.                             = in->default_value * 100 / IP_GET_SENSITIVITY(in);
  1443.                     }
  1444.                 }
  1445.                 else
  1446.                 {
  1447.                     InputSeq* seq;
  1448.  
  1449.                     seq = input_port_seq(in);
  1450.  
  1451.                     if (seq_pressed(seq))
  1452.                     {
  1453.                         /* skip if coin input and it's locked out */
  1454.                         if ((in->type & ~IPF_MASK) >= IPT_COIN1 &&
  1455.                             (in->type & ~IPF_MASK) <= IPT_COIN4 &&
  1456.                             coinlockedout[(in->type & ~IPF_MASK) - IPT_COIN1])
  1457.                         {
  1458.                             continue;
  1459.                         }
  1460.  
  1461.                         /* if IPF_RESET set, reset the first CPU */
  1462.                         if ((in->type & IPF_RESETCPU) && waspressed[ib] == 0)
  1463.                             cpu_set_reset_line(0,PULSE_LINE);
  1464.  
  1465.                         if (in->type & IPF_IMPULSE)
  1466.                         {
  1467. if (IP_GET_IMPULSE(in) == 0)
  1468.     logerror("error in input port definition: IPF_IMPULSE with length = 0\n");
  1469.                             if (waspressed[ib] == 0)
  1470.                                 impulsecount[ib] = IP_GET_IMPULSE(in);
  1471.                                 /* the input bit will be toggled later */
  1472.                         }
  1473.                         else if (in->type & IPF_TOGGLE)
  1474.                         {
  1475.                             if (waspressed[ib] == 0)
  1476.                             {
  1477.                                 in->default_value ^= in->mask;
  1478.                                 input_port_value[port] ^= in->mask;
  1479.                             }
  1480.                         }
  1481.                         else if ((in->type & ~IPF_MASK) >= IPT_JOYSTICK_UP &&
  1482.                                 (in->type & ~IPF_MASK) <= IPT_JOYSTICKLEFT_RIGHT)
  1483.                         {
  1484. #ifndef MAME_NET
  1485.                             int joynum,joydir,mask,player;
  1486.  
  1487.  
  1488.                             player = 0;
  1489.                             if ((in->type & IPF_PLAYERMASK) == IPF_PLAYER2) player = 1;
  1490.                             else if ((in->type & IPF_PLAYERMASK) == IPF_PLAYER3) player = 2;
  1491.                             else if ((in->type & IPF_PLAYERMASK) == IPF_PLAYER4) player = 3;
  1492. #else
  1493.                             int joynum,joydir,mask;
  1494. #endif /* !MAME_NET */
  1495.                             joynum = player * MAX_JOYSTICKS +
  1496.                                     ((in->type & ~IPF_MASK) - IPT_JOYSTICK_UP) / 4;
  1497.                             joydir = ((in->type & ~IPF_MASK) - IPT_JOYSTICK_UP) % 4;
  1498.  
  1499.                             mask = in->mask;
  1500.  
  1501. #ifndef MRU_JOYSTICK
  1502.                             /* avoid movement in two opposite directions */
  1503.                             if (joystick[joynum][joydir ^ 1] != 0)
  1504.                                 mask = 0;
  1505.                             else if (in->type & IPF_4WAY)
  1506.                             {
  1507.                                 int dir;
  1508.  
  1509.  
  1510.                                 /* avoid diagonal movements */
  1511.                                 for (dir = 0;dir < 4;dir++)
  1512.                                 {
  1513.                                     if (joystick[joynum][dir] != 0)
  1514.                                         mask = 0;
  1515.                                 }
  1516.                             }
  1517.  
  1518.                             joystick[joynum][joydir] = 1;
  1519. #else
  1520.                             /* avoid movement in two opposite directions */
  1521.                             if (joyserial[joynum][joydir ^ 1] != 0)
  1522.                                 mask = 0;
  1523.                             else if (in->type & IPF_4WAY)
  1524.                             {
  1525.                                 int mru_dir = joydir;
  1526.                                 int mru_serial = 0;
  1527.                                 int dir;
  1528.  
  1529.  
  1530.                                 /* avoid diagonal movements, use mru button */
  1531.                                 for (dir = 0;dir < 4;dir++)
  1532.                                 {
  1533.                                     if (joyserial[joynum][dir] > mru_serial)
  1534.                                     {
  1535.                                         mru_serial = joyserial[joynum][dir];
  1536.                                         mru_dir = dir;
  1537.                                     }
  1538.                                 }
  1539.  
  1540.                                 if (mru_dir != joydir)
  1541.                                     mask = 0;
  1542.                             }
  1543. #endif
  1544.  
  1545.                             input_port_value[port] ^= mask;
  1546.                         }
  1547.                         else
  1548.                             input_port_value[port] ^= in->mask;
  1549.  
  1550.                         waspressed[ib] = 1;
  1551.                     }
  1552.                     else
  1553.                         waspressed[ib] = 0;
  1554.  
  1555.                     if ((in->type & IPF_IMPULSE) && impulsecount[ib] > 0)
  1556.                     {
  1557.                         impulsecount[ib]--;
  1558.                         waspressed[ib] = 1;
  1559.                         input_port_value[port] ^= in->mask;
  1560.                     }
  1561.                 }
  1562.             }
  1563.         }
  1564.  
  1565.         port++;
  1566.         if (in->type == IPT_PORT) in++;
  1567.     }
  1568.  
  1569.     if (playback)
  1570.     {
  1571.         int i;
  1572.  
  1573.         for (i = 0; i < MAX_INPUT_PORTS; i ++)
  1574.             readword(playback,&input_port_value[i]);
  1575.     }
  1576.     if (record)
  1577.     {
  1578.         int i;
  1579.  
  1580.         for (i = 0; i < MAX_INPUT_PORTS; i ++)
  1581.             writeword(record,input_port_value[i]);
  1582.     }
  1583. #ifdef MAME_NET
  1584.     if ( net_active() && (default_player != NET_SPECTATOR) )
  1585.         net_input_sync((unsigned char *) input_port_value, (unsigned char *) input_port_defaults, MAX_INPUT_PORTS);
  1586. #endif /* MAME_NET */
  1587.  
  1588. profiler_mark(PROFILER_END);
  1589. }
  1590.  
  1591.  
  1592.  
  1593. /* used the the CPU interface to notify that VBlank has ended, so we can update */
  1594. /* IPT_VBLANK input ports. */
  1595. void inputport_vblank_end(void)
  1596. {
  1597.     int port;
  1598.     int i;
  1599.  
  1600.  
  1601. profiler_mark(PROFILER_INPUT);
  1602.     for (port = 0;port < MAX_INPUT_PORTS;port++)
  1603.     {
  1604.         if (input_vblank[port])
  1605.         {
  1606.             input_port_value[port] ^= input_vblank[port];
  1607.             input_vblank[port] = 0;
  1608.         }
  1609.     }
  1610.  
  1611.     /* poll all the analog joysticks */
  1612.     osd_poll_joysticks();
  1613.  
  1614.     /* update the analog devices */
  1615.     for (i = 0;i < OSD_MAX_JOY_ANALOG;i++)
  1616.     {
  1617.         /* update the analog joystick position */
  1618.         analog_previous_x[i] = analog_current_x[i];
  1619.         analog_previous_y[i] = analog_current_y[i];
  1620.         osd_analogjoy_read (i, &(analog_current_x[i]), &(analog_current_y[i]));
  1621.  
  1622.         /* update mouse/trackball position */
  1623.         osd_trak_read (i, &mouse_delta_x[i], &mouse_delta_y[i]);
  1624.     }
  1625.  
  1626.     for (i = 0;i < MAX_INPUT_PORTS;i++)
  1627.     {
  1628.         struct InputPort *in;
  1629.  
  1630.         in=input_analog[i];
  1631.         if (in)
  1632.         {
  1633.             update_analog_port(i);
  1634.         }
  1635.     }
  1636. profiler_mark(PROFILER_END);
  1637. }
  1638.  
  1639.  
  1640.  
  1641. int readinputport(int port)
  1642. {
  1643.     struct InputPort *in;
  1644.  
  1645.     /* Update analog ports on demand */
  1646.     in=input_analog[port];
  1647.     if (in)
  1648.     {
  1649.         scale_analog_port(port);
  1650.     }
  1651.  
  1652.     return input_port_value[port];
  1653. }
  1654.  
  1655. READ_HANDLER( input_port_0_r ) { return readinputport(0); }
  1656. READ_HANDLER( input_port_1_r ) { return readinputport(1); }
  1657. READ_HANDLER( input_port_2_r ) { return readinputport(2); }
  1658. READ_HANDLER( input_port_3_r ) { return readinputport(3); }
  1659. READ_HANDLER( input_port_4_r ) { return readinputport(4); }
  1660. READ_HANDLER( input_port_5_r ) { return readinputport(5); }
  1661. READ_HANDLER( input_port_6_r ) { return readinputport(6); }
  1662. READ_HANDLER( input_port_7_r ) { return readinputport(7); }
  1663. READ_HANDLER( input_port_8_r ) { return readinputport(8); }
  1664. READ_HANDLER( input_port_9_r ) { return readinputport(9); }
  1665. READ_HANDLER( input_port_10_r ) { return readinputport(10); }
  1666. READ_HANDLER( input_port_11_r ) { return readinputport(11); }
  1667. READ_HANDLER( input_port_12_r ) { return readinputport(12); }
  1668. READ_HANDLER( input_port_13_r ) { return readinputport(13); }
  1669. READ_HANDLER( input_port_14_r ) { return readinputport(14); }
  1670. READ_HANDLER( input_port_15_r ) { return readinputport(15); }
  1671.  
  1672. #ifdef MAME_NET
  1673. void set_default_player_controls(int player)
  1674. {
  1675.     if (player == NET_SPECTATOR)
  1676.         default_player = NET_SPECTATOR;
  1677.     else
  1678.         default_player = player - 1;
  1679. }
  1680. #endif /* MAME_NET */
  1681.  
  1682. /***************************************************************************/
  1683. /* InputPort conversion */
  1684.  
  1685. static unsigned input_port_count(const struct InputPortTiny *src)
  1686. {
  1687.     unsigned total;
  1688.  
  1689.     total = 0;
  1690.     while (src->type != IPT_END)
  1691.     {
  1692.         int type = src->type & ~IPF_MASK;
  1693.         if (type > IPT_ANALOG_START && type < IPT_ANALOG_END)
  1694.             total += 2;
  1695.         else if (type != IPT_EXTENSION)
  1696.             ++total;
  1697.         ++src;
  1698.     }
  1699.  
  1700.     ++total; /* for IPT_END */
  1701.  
  1702.     return total;
  1703. }
  1704.  
  1705. struct InputPort* input_port_allocate(const struct InputPortTiny *src)
  1706. {
  1707.     struct InputPort* dst;
  1708.     struct InputPort* base;
  1709.     unsigned total;
  1710.  
  1711.     total = input_port_count(src);
  1712.  
  1713.     base = (struct InputPort*)malloc(total * sizeof(struct InputPort));
  1714.     dst = base;
  1715.  
  1716.     while (src->type != IPT_END)
  1717.     {
  1718.         int type = src->type & ~IPF_MASK;
  1719.         const struct InputPortTiny *ext;
  1720.         const struct InputPortTiny *src_end;
  1721.         InputCode seq_default;
  1722.  
  1723.         if (type > IPT_ANALOG_START && type < IPT_ANALOG_END)
  1724.             src_end = src + 2;
  1725.         else
  1726.             src_end = src + 1;
  1727.  
  1728.         switch (type)
  1729.         {
  1730.             case IPT_END :
  1731.             case IPT_PORT :
  1732.             case IPT_DIPSWITCH_NAME :
  1733.             case IPT_DIPSWITCH_SETTING :
  1734.                 seq_default = CODE_NONE;
  1735.             break;
  1736.             default:
  1737.                 seq_default = CODE_DEFAULT;
  1738.         }
  1739.  
  1740.         ext = src_end;
  1741.         while (src != src_end)
  1742.         {
  1743.             dst->type = src->type;
  1744.             dst->mask = src->mask;
  1745.             dst->default_value = src->default_value;
  1746.             dst->name = src->name;
  1747.  
  1748.               if (ext->type == IPT_EXTENSION)
  1749.               {
  1750.                 InputCode or1 =    IP_GET_CODE_OR1(ext);
  1751.                 InputCode or2 =    IP_GET_CODE_OR2(ext);
  1752.  
  1753.                 if (or1 < __code_max)
  1754.                 {
  1755.                     if (or2 < __code_max)
  1756.                         seq_set_3(&dst->seq, or1, CODE_OR, or2);
  1757.                     else
  1758.                         seq_set_1(&dst->seq, or1);
  1759.                 } else {
  1760.                     if (or1 == CODE_NONE)
  1761.                         seq_set_1(&dst->seq, or2);
  1762.                     else
  1763.                         seq_set_1(&dst->seq, or1);
  1764.                 }
  1765.  
  1766.                   ++ext;
  1767.               } else {
  1768.                 seq_set_1(&dst->seq,seq_default);
  1769.               }
  1770.  
  1771.             ++src;
  1772.             ++dst;
  1773.         }
  1774.  
  1775.         src = ext;
  1776.     }
  1777.  
  1778.     dst->type = IPT_END;
  1779.  
  1780.     return base;
  1781. }
  1782.  
  1783. void input_port_free(struct InputPort* dst)
  1784. {
  1785.     free(dst);
  1786. }
  1787.  
  1788.